package com.iqiyi.pps.epg.core.service.rtmp;

import com.iqiyi.pps.epg.core.dao.rtmp.RtmpServerDao;
import com.iqiyi.pps.epg.core.dao.rtmp.RtmpServerPfvDao;
import com.iqiyi.pps.epg.core.dao.rtmp.RtmpServerStatDao;
import com.iqiyi.pps.epg.core.model.rtmp.RtmpServer;
import com.iqiyi.pps.epg.core.model.rtmp.RtmpServerPfv;
import com.iqiyi.pps.epg.core.model.rtmp.RtmpServerStat;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RtmpServiceImpl implements RtmpService {
    @Autowired
    private RtmpServerDao rtmpServerDao;
    @Autowired
    private RtmpServerStatDao rtmpServerStatDao;
    @Autowired
    private RtmpServerPfvDao rtmpServerPfvDao;

    @Override
    public RtmpServer getServerById(long id) {
        return rtmpServerDao.get(id);
    }

    @Override
    public RtmpServerStat getServerStatById(long id) {
        return rtmpServerStatDao.get(id);
    }

    @Override
    public boolean saveServer(RtmpServer entity) {
        try {
            rtmpServerDao.save(entity);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean saveServerStat(RtmpServerStat entity) {
        try {
            rtmpServerStatDao.save(entity);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean saveServerPfv(RtmpServerPfv entity) {
        try {
            rtmpServerPfvDao.save(entity);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public List<RtmpServer> getServerList() {
        Criteria criteria = rtmpServerDao.createCriteria();
        criteria.add(Restrictions.eq("status", RtmpServer.STATUS_ENABLE));
        return rtmpServerDao.find(criteria);
    }

    @Override
    public List<RtmpServerStat> getServerStatListByServerId(int serverId) {
        Criteria criteria = rtmpServerStatDao.createCriteria();
        criteria.add(Restrictions.eq("serverId", serverId));
        return rtmpServerStatDao.find(criteria);
    }

    @Override
    public List<RtmpServerStat> getServerStatList() {
        return rtmpServerStatDao.getAll();
    }

    @Override
    public List<RtmpServerPfv> getServerPfvList() {
        return rtmpServerPfvDao.getAll();
    }

    @Override
    public boolean isValidStream(String streamName) {
        Criteria criteria = rtmpServerStatDao.createCriteria();
        criteria.add(Restrictions.gt("serverId", 0));
        criteria.add(Restrictions.eq("name", streamName));
        criteria.add(Restrictions.eq("status", RtmpServerStat.STATUS_VALID));
        return rtmpServerStatDao.countCriteriaResult(criteria) > 0 ? true : false;
//        String sql = "select count(*) from rtmp_server_stat server_stat left join rtmp_server server on server.id = server_stat.server_id where server.public_ip=:ip and server_stat.name=:name and server_stat.status=:status";
//        return ((Long) rtmpServerStatDao.getSession()
//            .createSQLQuery(sql)
//            .setParameter("ip", serverIp)
//            .setParameter("name", streamName)
//            .setParameter("status", RtmpServerStat.STATUS_VALID)
//            .uniqueResult()) > 0 ? true : false;
    }

    @Override
    public List<RtmpServerStat> streamInfoSuggest(String streamName) {
        // return rtmpServerStatDao.streamInfoSuggest(streamName + "%");
        return rtmpServerStatDao.getListByName(streamName);
    }

    @Override
    public RtmpServer getByPrivateIp(String privateIp) {
        Criteria criteria = rtmpServerDao.createCriteria();
        criteria.add(Restrictions.eq("status", RtmpServer.STATUS_ENABLE));
        criteria.add(Restrictions.eq("privateIp", privateIp));
        return  rtmpServerDao.findUnique(criteria);
    }

    @Override
    public RtmpServer getByPublicIp(String publicIp) {
        Criteria criteria = rtmpServerDao.createCriteria();
        criteria.add(Restrictions.eq("status", RtmpServer.STATUS_ENABLE));
        criteria.add(Restrictions.eq("publicIp", publicIp));
        return  rtmpServerDao.findUnique(criteria);
    }


}
